
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.utils.module_loading &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-utils-module_loading">
            
  <h1>Source code for django.utils.module_loading</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">importlib</span> <span class="k">import</span> <span class="n">import_module</span>
<span class="kn">from</span> <span class="nn">importlib.util</span> <span class="k">import</span> <span class="n">find_spec</span> <span class="k">as</span> <span class="n">importlib_find</span>


<div class="viewcode-block" id="import_string"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.module_loading.import_string">[docs]</a><span class="k">def</span> <span class="nf">import_string</span><span class="p">(</span><span class="n">dotted_path</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Import a dotted module path and return the attribute/class designated by the</span>
<span class="sd">    last name in the path. Raise ImportError if the import failed.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">module_path</span><span class="p">,</span> <span class="n">class_name</span> <span class="o">=</span> <span class="n">dotted_path</span><span class="o">.</span><span class="n">rsplit</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">ValueError</span> <span class="k">as</span> <span class="n">err</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ImportError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> doesn&#39;t look like a module path&quot;</span> <span class="o">%</span> <span class="n">dotted_path</span><span class="p">)</span> <span class="kn">from</span> <span class="nn">err</span>

    <span class="n">module</span> <span class="o">=</span> <span class="n">import_module</span><span class="p">(</span><span class="n">module_path</span><span class="p">)</span>

    <span class="k">try</span><span class="p">:</span>
        <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">module</span><span class="p">,</span> <span class="n">class_name</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">AttributeError</span> <span class="k">as</span> <span class="n">err</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ImportError</span><span class="p">(</span><span class="s1">&#39;Module &quot;</span><span class="si">%s</span><span class="s1">&quot; does not define a &quot;</span><span class="si">%s</span><span class="s1">&quot; attribute/class&#39;</span> <span class="o">%</span> <span class="p">(</span>
            <span class="n">module_path</span><span class="p">,</span> <span class="n">class_name</span><span class="p">)</span>
        <span class="p">)</span> <span class="kn">from</span> <span class="nn">err</span></div>


<span class="k">def</span> <span class="nf">autodiscover_modules</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Auto-discover INSTALLED_APPS modules and fail silently when</span>
<span class="sd">    not present. This forces an import on them to register any admin bits they</span>
<span class="sd">    may want.</span>

<span class="sd">    You may provide a register_to keyword parameter as a way to access a</span>
<span class="sd">    registry. This register_to object must have a _registry instance variable</span>
<span class="sd">    to access it.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="kn">from</span> <span class="nn">django.apps</span> <span class="k">import</span> <span class="n">apps</span>

    <span class="n">register_to</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;register_to&#39;</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">app_config</span> <span class="ow">in</span> <span class="n">apps</span><span class="o">.</span><span class="n">get_app_configs</span><span class="p">():</span>
        <span class="k">for</span> <span class="n">module_to_search</span> <span class="ow">in</span> <span class="n">args</span><span class="p">:</span>
            <span class="c1"># Attempt to import the app&#39;s module.</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">register_to</span><span class="p">:</span>
                    <span class="n">before_import_registry</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">register_to</span><span class="o">.</span><span class="n">_registry</span><span class="p">)</span>

                <span class="n">import_module</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">app_config</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">module_to_search</span><span class="p">))</span>
            <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                <span class="c1"># Reset the registry to the state before the last import</span>
                <span class="c1"># as this import will have to reoccur on the next request and</span>
                <span class="c1"># this could raise NotRegistered and AlreadyRegistered</span>
                <span class="c1"># exceptions (see #8245).</span>
                <span class="k">if</span> <span class="n">register_to</span><span class="p">:</span>
                    <span class="n">register_to</span><span class="o">.</span><span class="n">_registry</span> <span class="o">=</span> <span class="n">before_import_registry</span>

                <span class="c1"># Decide whether to bubble up this error. If the app just</span>
                <span class="c1"># doesn&#39;t have the module in question, we can ignore the error</span>
                <span class="c1"># attempting to import it, otherwise we want it to bubble up.</span>
                <span class="k">if</span> <span class="n">module_has_submodule</span><span class="p">(</span><span class="n">app_config</span><span class="o">.</span><span class="n">module</span><span class="p">,</span> <span class="n">module_to_search</span><span class="p">):</span>
                    <span class="k">raise</span>


<span class="k">def</span> <span class="nf">module_has_submodule</span><span class="p">(</span><span class="n">package</span><span class="p">,</span> <span class="n">module_name</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;See if &#39;module&#39; is in &#39;package&#39;.&quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">package_name</span> <span class="o">=</span> <span class="n">package</span><span class="o">.</span><span class="vm">__name__</span>
        <span class="n">package_path</span> <span class="o">=</span> <span class="n">package</span><span class="o">.</span><span class="n">__path__</span>
    <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
        <span class="c1"># package isn&#39;t a package.</span>
        <span class="k">return</span> <span class="kc">False</span>

    <span class="n">full_module_name</span> <span class="o">=</span> <span class="n">package_name</span> <span class="o">+</span> <span class="s1">&#39;.&#39;</span> <span class="o">+</span> <span class="n">module_name</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">importlib_find</span><span class="p">(</span><span class="n">full_module_name</span><span class="p">,</span> <span class="n">package_path</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
    <span class="k">except</span> <span class="p">(</span><span class="ne">ImportError</span><span class="p">,</span> <span class="ne">AttributeError</span><span class="p">):</span>
        <span class="c1"># When module_name is an invalid dotted path, Python raises ImportError</span>
        <span class="c1"># (or ModuleNotFoundError in Python 3.6+). AttributeError may be raised</span>
        <span class="c1"># if the penultimate part of the path is not a package.</span>
        <span class="c1"># (https://bugs.python.org/issue30436)</span>
        <span class="k">return</span> <span class="kc">False</span>


<span class="k">def</span> <span class="nf">module_dir</span><span class="p">(</span><span class="n">module</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Find the name of the directory that contains a module, if possible.</span>

<span class="sd">    Raise ValueError otherwise, e.g. for namespace packages that are split</span>
<span class="sd">    over several directories.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># Convert to list because _NamespacePath does not support indexing on 3.3.</span>
    <span class="n">paths</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">module</span><span class="p">,</span> <span class="s1">&#39;__path__&#39;</span><span class="p">,</span> <span class="p">[]))</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">paths</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">paths</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">filename</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">module</span><span class="p">,</span> <span class="s1">&#39;__file__&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">filename</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Cannot determine directory containing </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">module</span><span class="p">)</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>